package com.ruoyi.web.controller.useradmin;

import com.ruoyi.useradmin.domain.UaUser;
import com.ruoyi.useradmin.service.IUaUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.IdList.idList;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.service.ISysUserService;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Description  
 * @author kemonvvv
 * @Date 2023-06-05 
 */
@RestController
@RequestMapping("useradmin/uaUser")
public class UaUserController extends BaseController {
    @Resource
    private ISysUserService  sysUserService;

    @Resource
    private IUaUserService uaUserService;
    /**
     * 通过全查询
     *  params是用若依的特殊处理过的，不能直接用url去测试，只能用若依的主键写代码去测试
     */
    @PreAuthorize("@ss.hasPermi('useradmin:uaUser:query')")
    @PostMapping("/getUaUserPage")
    public AjaxResult getUaUserPage(@RequestBody PageDomain pageDomain,@RequestBody UaUser uaUser) {
        Object data = uaUserService.getUaUserPage(pageDomain, uaUser);
        return AjaxResult.success(data);
    }

    /**
     * 通过查询全部
     */
    @PreAuthorize("@ss.hasPermi('useradmin:uaUser:query')")
    @PostMapping("/getUaUserList")
    public AjaxResult getUaUserList() {
        Object data = uaUserService.getUaUserList();
        return AjaxResult.success(data);
    }
    
    /**
     * 通过id查询
     */
    @PreAuthorize("@ss.hasPermi('useradmin:uaUser:query')")
    @PostMapping("/getUaUserById")
    public AjaxResult getUaUserById(@RequestBody Map<String,Long> idMap) {
        Object data = uaUserService.getUaUserById(idMap.get("id"));
        return AjaxResult.success(data);
    }

    /**
     * 添加
     */
    @PreAuthorize("@ss.hasPermi('useradmin:uaUser:add')")
    @PostMapping("/insertUaUser")
    public AjaxResult insertUaUser(@RequestBody UaUser uaUser) {
        Object data = uaUserService.insertUaUser(uaUser);
        if (!Objects.isNull(uaUser.getOpenid())){
            SysUser sysUser = new SysUser();
            sysUser.setNickName(uaUser.getOpenid());
            sysUser.setUserName(uaUser.getOpenid());
            sysUser.setPassword(SecurityUtils.encryptPassword("admin123"));
            sysUser.setUserInfoType("2");
            sysUser.setUserInfoId(uaUser.getId());
            sysUserService.insertUser(sysUser);
        }
        return AjaxResult.success(data);
    }

    /**
     * 更新
     */
    @PreAuthorize("@ss.hasPermi('useradmin:uaUser:edit')")
    @PostMapping("/updateUaUser")
    public AjaxResult updateUaUser(@RequestBody UaUser uaUser) {
        Object data = uaUserService.updateUaUserById(uaUser);
        return AjaxResult.success(data);
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('useradmin:uaUser:remove')")
    @PostMapping("/deleteUaUserByIdList")
    public AjaxResult deleteUaUserByIdList(@RequestBody idList idList) {
        Object data = uaUserService.deleteUaUserByIdList(idList.getIdList());
        return AjaxResult.success(data);
    }

}
